package com.twu.biblioteca.librarycollection;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Maximilian
 * Date: 2/10/13
 * Time: 7:37 PM
 * To change this template use File | Settings | File Templates.
 */
public class MovieCollection {

    public static void main (String [] args) throws Exception
    {
        MovieCollection movieColl = new MovieCollection();
        movieColl.addMovie("MovieA", "1982", "Raymond", 10);
        movieColl.addMovie("A different movie", "1982", "Ho", 2);
        movieColl.addMovie("Mo", "1982", "Le rouge de rouge", 1);
        System.out.println(movieColl.getMovieListString());
    }

    protected class Movie
    {
        public String name, year, director;
        public int rating;

        public Movie(String name, String year, String director, int rating)
        {
            if(year.length() != 4 || Integer.valueOf(year) < 1900 || Integer.valueOf(year) > getCurYear())
            {
                throw new IncorrectYearFormatException(year + " is not properly formatted year");
            }
            this.name = name;
            this.year = year;
            this.director = director;
            this.rating = rating;
        }

        //In reality this would use system time or equivalent
        private int getCurYear()
        {
            return 2013;
        }
    }

    Map <String, Movie> nameToMovie = new HashMap<String, Movie>();
    List <String> movieNamesInOrder = new ArrayList <String>();

    public void addMovie(String title, String year, String director, int rating) throws DuplicateMovieException
    {
        if(rating > 10 || rating < 1)
            throw new ImproperRatingException("Rating " + rating + " is not inside range from 1 to 10");
        Movie movie = new Movie(title, year, director, rating);
        if(nameToMovie.containsKey(title))
        {
            throw new DuplicateMovieException("Already have movie called " + title);
        }
        nameToMovie.put(title, new Movie(title, year, director, rating));
        movieNamesInOrder.add(title);
    }

    public String getMovieListString()
    {
        List <Movie> movies = new ArrayList <Movie>();

        for(String title : movieNamesInOrder)
        {
            movies.add(nameToMovie.get(title));
        }

        return MovieFormatter.generateFormattedMovieList(movies);
    }

}
